##############################################################
#Figure C3: Distribution of Risk Aversion and Education in CCES 2014
##############################################################

data <- read.dta("./input/cces-subset.dta")

smalld <- data[,c("employed", "out_of_labour", "race_14", "risk", "dem14", "education", 
                  "female", "income", "income_quarters", "age")]

smalld$income <- as.factor(smalld$income)
smalld$income_quarters <- as.factor(smalld$income_quarters)
smalld$education <- as.factor(smalld$education)

data2 <- na.omit(smalld) 

data2.2 <- data2 %>% 
  group_by(education,risk) %>% 
  summarise(count=n()) %>% 
  mutate(perc=count/sum(count))

data2.2$educ2 = factor(data2.2$education, levels=c(0,1,2,3), labels =c('No High School','High School','Tertiary Education','Post Grad Education'))


ggplot(data2.2, aes(x=as.factor(risk), y=perc, group=as.factor(education), fill=as.factor(education))) +
  geom_bar(stat= "identity", position="dodge") + 
  scale_y_continuous(labels=percent) + 
  facet_grid(~educ2)  + 
  ylab("Percentage") + 
  xlab ("Risk Aversion")  + 
  theme_bw() + 
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor =   element_blank(),
        legend.position = "none") + 
  scale_x_discrete(breaks=c(0,1,2,3),labels= c("Taker", "", "", "Averse"))  +
  scale_fill_manual(values=c("grey60", "grey60", "grey60", "grey60"))

ggsave("./figures/figc3.pdf")  